//
//  GW1830.m
//  X2a
//
//  Created by Jack.MT on 15/9/15.
//  Copyright (c) 2015年 Jack.MT. All rights reserved.
//

#import "GWPower.h"

@implementation GWPower

-(BOOL) OpenDevice
{
    _opened = NO;
    BOOL isFind = [self Find];
    
    if (!isFind)
    {
        return NO;
    }
    /* Now we will open a session to the instrument we just found. */
    status = viOpen (defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr);
    
    if (status < VI_SUCCESS)
    {
        return NO;
    }
    
    _opened = YES;
    
    return YES;
}

-(void)CloseDevice
{
    /* Now close the session we just opened.                            */
    /* In actuality, we would probably use an attribute to determine    */
    /* if this is the instrument we are looking for.                    */
    viClose (instr);
    status = viClose(findList);
    status = viClose (defaultRM);
    _opened = NO;
}

-(BOOL) sendCommand:(NSString*) command
{
    /*
     * At this point we now have a session open to the instrument at
     * Primary Address 2.  We can use this session handle to write
     * an ASCII command to the instrument.  We will use the viWrite function
     * to send the string "*IDN?", asking for the device's identification.
     */
    status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 8000);
    
    @autoreleasepool {
        NSString* dataLine = [[NSString alloc] initWithFormat:@"%@\n", command];
        NSInteger dataLen = [dataLine length];
        char inputBuffer[dataLen];
        strcpy(inputBuffer, [dataLine UTF8String]);
        
        status = viWrite (instr, (ViBuf)inputBuffer, (ViUInt32)strlen(inputBuffer), &writeCount);
        
        // [dataLine release];
        
        if (status != VI_SUCCESS)
        {
            return NO;
        }
        
        return YES;
    }
}

-(NSString *)readDataByCommand:(NSString *)command
{
    [self sendCommand:command];
    return [self readDataWithCountOfByte:1024];
}

-(NSString*) readDataWithCountOfByte:(int)byteCount
{
    unsigned char buffer[byteCount];
    memset(buffer, 0, byteCount);
    status = viRead (instr, buffer, byteCount, &retCount);
    
    if (status < VI_SUCCESS)
    {
        return @"";
    }
    
    
    @autoreleasepool {
        
        NSString *result = [[NSString alloc] initWithUTF8String:(const char*)buffer];
        
        return result;
    }
}

-(BOOL) Find
{
    status = viOpenDefaultRM (&defaultRM);
    
    if (status < VI_SUCCESS)
    {
        exit (EXIT_FAILURE);
    }
    
    /*
     * Find all the VISA resources in our system and store the number of resources
     * in the system in numInstrs.  Notice the different query descriptions a
     * that are available.
     
     Interface         Expression
     --------------------------------------
     GPIB              "GPIB[0-9]*::?*INSTR"
     VXI               "VXI?*INSTR"
     GPIB-VXI          "GPIB-VXI?*INSTR"
     Any VXI           "?*VXI[0-9]*::?*INSTR"
     Serial            "ASRL[0-9]*::?*INSTR"
     PXI               "PXI?*INSTR"
     All instruments   "?*INSTR"
     All resources     "?*"
     */
    status = viFindRsrc (defaultRM, "GPIB0::5::INSTR"/*"USB0::0x0957::0x0607::?*"*/, &findList, &numInstrs, instrDescriptor);
    
    if (status < VI_SUCCESS)
    {
        printf ("An error occurred while finding resources.\n");
        fflush(stdin);
        //       getchar();
        viClose (defaultRM);
        return NO;
    }
    
    NSLog(@"%s",instrDescriptor);
    
    return YES;
}

@end
